﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallDataBase.Attribute;
using SmallDataBase;
using System.Threading;
using TestConsoleApplication;
using SmallDataBase.DTO;

namespace TestConsoleApplication
{

    public class TestClass
    {
        public static SQLHelper helper = new SQLHelper();
          static TestClass()
          {
              helper.WatchSQL += new SQLHelper.WatchSQLHandler(helper_WatchSQL);
          }


        //0.0.0.1耗时约20秒，多线程耗时约8秒
        public static void Insert1000Record()
        {
            int limitMax = 1000;
            var list = new List<Entity>();
            var t1 = DateTime.Now;
            for (int i = 0; i < limitMax; i++)
            {
                list.Add(GetEntity());
            }
            Console.WriteLine("单线初始实例用时：" + DateTime.Now.Subtract(t1));
            CountdownEvent countdown = new CountdownEvent(limitMax);
            t1 = DateTime.Now;

            //for (int i = 0; i < limitMax; i++)
            //{
            //    ThreadPool.QueueUserWorkItem(o =>
            //    {
            //        helper.Insert(o);
            //        countdown.Signal();
            //    }, list[i]);
            //}
            //countdown.Wait();
            //Console.WriteLine("多线插入数据库用时：" + DateTime.Now.Subtract(t1));

            for (int i = 0; i < limitMax; i++)
            {
                helper.Insert(list[i]);
            }
            Console.WriteLine("单线插入数据库用时：" + DateTime.Now.Subtract(t1));
        }

        public static  void InsertRecord()
        {

            var item = GetEntity();
            helper.Insert(item);
           
            Console.WriteLine("插入项ID为：{0}", item.ID);
            var item2 = GetEntity();
            helper.InsertAndInitID(item2);
            Console.WriteLine("插入项ID为：{0}", item2.ID);
        }

        static void helper_WatchSQL(string sql)
        {
            Console.WriteLine("event:" + sql);
        }

        public static void Query1000Record()
        {
            var time1 = DateTime.Now;
            var @where = new SQLQueryGroupDTO(GroupType.Or);
            @where.SQLQueryDTOs.Add(new SQLQueryDTO("Address", "%fc%", SQLOperators.Like));
            @where.SQLQueryDTOs.Add(new SQLQueryDTO("Age", "70", SQLOperators.GreaterThan));
            var list = helper.QueryObjects<Entity>(@where);
            Console.WriteLine("查询" + list.Count + "条记录耗时：" + DateTime.Now.Subtract(time1));
        }

        public static void Query1000RecordNoWhere()
        {
            var time1 = DateTime.Now;
            
            var list = helper.QueryObjects<Entity>();
            Console.WriteLine("查询" + list.Count + "条记录耗时：" + DateTime.Now.Subtract(time1));
        }

        public static void DeleteRecord()
        {
            var @where = new SQLQueryGroupDTO(GroupType.Or);
            @where.SQLQueryDTOs.Add(new SQLQueryDTO("Address", "%za%", SQLOperators.Like));
            @where.SQLQueryDTOs.Add(new SQLQueryDTO("Age", "50", SQLOperators.LessThan));
            helper.Delete<Entity>(@where);
        }


        private static Entity GetEntity()
        {
            var entity = new Entity()
                             {
                                 Score = (float) random.NextDouble()*99,
                                 Address = GetRandomStr(50, 250),
                                 Age = random.Next(100),
                                 MyName = GetRandomStr(6, 30),
                                 Password = GetRandomStr(6, 50)
                             };
            return entity;
        }
        private  static  Random random = new Random();
        private static string GetRandomStr(int min,int max)
        {
            StringBuilder sb = new StringBuilder();
            string seed = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
            var length = random.Next(min, max);
            for (int i = 0; i < length; i++)
            {

                sb.Append(seed[random.Next(seed.Length)]);
            }
            return sb.ToString();
        }




    }
}
